package com.rskj.cashbook.controller;

import com.rskj.cashbook.response.ResponseEntity;
import com.rskj.cashbook.service.IDictService;
import com.rskj.cashbook.vo.BillTypeDTO;
import com.rskj.cashbook.vo.Dict;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 账单类型控制器
 */
@RestController
@RequestMapping("/api/bill-types")
@Slf4j
@RequiredArgsConstructor
public class BillTypeController {
    
    private final IDictService dictService;
    
    /**
     * 获取所有账单类型
     */
    @GetMapping
    public ResponseEntity getAllBillTypes() {
        try {
            List<Dict> dicts = dictService.getByCategory("BILL_TYPE");
            List<BillTypeDTO> billTypes = dicts.stream()
                .map(BillTypeDTO::new)
                .collect(Collectors.toList());
            return ResponseEntity.ok(billTypes);
        } catch (Exception e) {
            log.error("获取账单类型列表失败", e);
            return ResponseEntity.failGeneric("获取账单类型列表失败");
        }
    }
    
    /**
     * 根据分类获取账单类型
     */
    @GetMapping("/category/{category}")
    public ResponseEntity getBillTypesByCategory(@PathVariable String category) {
        try {
            List<Dict> dicts = dictService.getByCategoryAndParent("BILL_TYPE", category);
            List<BillTypeDTO> billTypes = dicts.stream()
                .map(BillTypeDTO::new)
                .collect(Collectors.toList());
            return ResponseEntity.ok(billTypes);
        } catch (Exception e) {
            log.error("根据分类获取账单类型失败，分类：{}", category, e);
            return ResponseEntity.fail("根据分类获取账单类型失败");
        }
    }
    
    /**
     * 获取支出类型
     */
    @GetMapping("/expense")
    public ResponseEntity getExpenseTypes() {
        try {
            List<Dict> dicts = dictService.getByCategoryAndParent("BILL_TYPE", "EXPENSE");
            List<BillTypeDTO> billTypes = dicts.stream()
                .map(BillTypeDTO::new)
                .collect(Collectors.toList());
            return ResponseEntity.ok(billTypes);
        } catch (Exception e) {
            log.error("获取支出类型失败", e);
            return ResponseEntity.fail("获取支出类型失败");
        }
    }
    
    /**
     * 获取收入类型
     */
    @GetMapping("/income")
    public ResponseEntity getIncomeTypes() {
        try {
            List<Dict> dicts = dictService.getByCategoryAndParent("BILL_TYPE", "INCOME");
            List<BillTypeDTO> billTypes = dicts.stream()
                .map(BillTypeDTO::new)
                .collect(Collectors.toList());
            return ResponseEntity.ok(billTypes);
        } catch (Exception e) {
            log.error("获取收入类型失败", e);
            return ResponseEntity.fail("获取收入类型失败");
        }
    }
    
    /**
     * 获取借贷类型
     */
    @GetMapping("/loan")
    public ResponseEntity getLoanTypes() {
        try {
            List<Dict> dicts = dictService.getByCategoryAndParent("BILL_TYPE", "LOAN");
            List<BillTypeDTO> billTypes = dicts.stream()
                .map(BillTypeDTO::new)
                .collect(Collectors.toList());
            return ResponseEntity.ok(billTypes);
        } catch (Exception e) {
            log.error("获取借贷类型失败", e);
            return ResponseEntity.fail("获取借贷类型失败");
        }
    }
    
    /**
     * 获取转账类型
     */
    @GetMapping("/transfer")
    public ResponseEntity getTransferTypes() {
        try {
            List<Dict> dicts = dictService.getByCategoryAndParent("BILL_TYPE", "TRANSFER");
            List<BillTypeDTO> billTypes = dicts.stream()
                .map(BillTypeDTO::new)
                .collect(Collectors.toList());
            return ResponseEntity.ok(billTypes);
        } catch (Exception e) {
            log.error("获取转账类型失败", e);
            return ResponseEntity.fail("获取转账类型失败");
        }
    }
}
